package com.huterox.todoscheduler.core.scheduler.impl;

import com.huterox.todoscheduler.common.SerializationUtils;
import com.huterox.todoscheduler.common.TaskManager;
import com.huterox.todoscheduler.common.impl.DefaultTaskManager;
import com.huterox.todoscheduler.config.ConfigEngine;
import com.huterox.todoscheduler.config.Configuration;
import com.huterox.todoscheduler.core.enumType.ExecuteType;
import com.huterox.todoscheduler.core.execute.impl.DefaultExecuteCore;
import com.huterox.todoscheduler.core.execute.proxy.TodoListExBean;
import com.huterox.todoscheduler.core.scheduler.SchedulerEx;
import com.huterox.todoscheduler.core.wapper.TaskWrapper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadPoolExecutor;

public class DefaultSchedulerEx implements SchedulerEx, Serializable {

    //默认启动的时候启动这个
    public DefaultSchedulerEx() {
        repairTask();
    }

    @Override
    public void repairTask() {
        //两步走战略
        //1. 先恢复执行线程当中的任务
        //2. 查看失败队列有没有任务，有的话运行

        try {
            //先等一会儿再启动
            Thread.sleep(Configuration.WaitingRepairTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Object deserializeObject = SerializationUtils
                .deserializeObject("runningTask", "task.ser");
        if(deserializeObject!=null){
            DefaultTaskManager runningTask = (DefaultTaskManager) deserializeObject;

            //拿到当时的任务队列
            ThreadPoolExecutor executor = runningTask.getExecutor();
            ArrayList<Runnable> runnableWrappers = new ArrayList<>(executor.getQueue());
            for(Runnable runnable:runnableWrappers){
                TaskWrapper taskWrapper = (TaskWrapper) runnable;
                taskWrapper.setExecuteType(ExecuteType.Repair);

                //重新提交
                TaskManager o = (TaskManager) ConfigEngine.corePart.get(TaskManager.class);
                o.submitTask(taskWrapper,taskWrapper.getExecuteCore().getClsId());
            }

        }

        againTask();
    }

    @Override
    public void againTask() {
        //重新执行失败任务

        Object deserializeObject = SerializationUtils
                .deserializeObject("failedCatalogue", "catalogue.ser");
        if (deserializeObject!=null){
            CopyOnWriteArrayList<TodoListExBean> failedCatalogue = (CopyOnWriteArrayList<TodoListExBean>) deserializeObject;

            for(TodoListExBean todoListExBean:failedCatalogue){
                if(todoListExBean.getExTimes()<Configuration.ExTimes){
                    DefaultExecuteCore defaultExecuteCore = new DefaultExecuteCore();
                    defaultExecuteCore.setTodoListExBean(todoListExBean);
                    TaskWrapper taskWrapper = new TaskWrapper();
                    taskWrapper.setExecuteCore(defaultExecuteCore);
                    //重新提交
                    TaskManager o = (TaskManager) ConfigEngine.corePart.get(TaskManager.class);
                    o.submitTask(taskWrapper,defaultExecuteCore.getClsId());
                }

            }
        }

    }

    @Override
    public void DaemonScheduling() {
        Integer schedulerSeconds = Configuration.schedulerSeconds;
        while (true) {
            try {
                Thread.sleep(Configuration.schedulerSeconds);
                againTask();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
